package com.zf.zfed.audit.ui

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.zf.zfed.audit.R

/**
 * 带右侧文字的设置项控件
 */
@SuppressLint("ResourceAsColor", "RtlHardcoded")
class TextSetItem @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    
    // 图标
    private var itemIcon: ImageView
    
    // 名称
    private var itemName: TextView
    
    // 右侧文字
    private var rightText: TextView

    // 分割线 - 已移除
    // private var line: View

    init {
        isClickable = true
        isFocusable = true
        orientation = VERTICAL
        
        // 设置卡片背景
        background = ContextCompat.getDrawable(context, R.drawable.card_background)
        
        // 设置卡片边距
        val cardPadding = context.dpFromDimen(R.dimen.spacing_8)
        setPadding(cardPadding, cardPadding, cardPadding, cardPadding)

        // 内嵌布局
        val nestedFrameLayout = FrameLayout(context)
        val layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F)
        addView(nestedFrameLayout, layoutParams)

        // 图标
        itemIcon = ImageView(context)
        val iconSize = context.dpFromDimen(R.dimen.size_22)
        val iconLP = FrameLayout.LayoutParams(iconSize, iconSize)
        iconLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        iconLP.leftMargin = context.dpFromDimen(R.dimen.spacing_16)
        // 设置图标缩放类型
        itemIcon.scaleType = ImageView.ScaleType.CENTER_INSIDE
        nestedFrameLayout.addView(itemIcon, iconLP)

        // 文案
        itemName = TextView(context)
        itemName.setTextColor(ContextCompat.getColor(context, R.color.text_primary))
        itemName.textSize = context.spFromDimen(R.dimen.font_20)
        itemName.setTypeface(null, android.graphics.Typeface.BOLD)
        val textLP = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        textLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        textLP.leftMargin = context.dpFromDimen(R.dimen.spacing_50) // 为图标留出空间
        nestedFrameLayout.addView(itemName, textLP)

        // 右侧文字
        rightText = TextView(context)
        rightText.setTextColor(ContextCompat.getColor(context, R.color.text_primary))
        rightText.textSize = context.spFromDimen(R.dimen.font_14)
        val rightTextLP = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        rightTextLP.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        rightTextLP.marginEnd = context.dpFromDimen(R.dimen.spacing_16)
        nestedFrameLayout.addView(rightText, rightTextLP)

        // 分割线 - 已移除
        // line = View(context)
        // line.setBackgroundColor(ContextCompat.getColor(context, R.color.solid_line))
        // val lineLP = LayoutParams(LayoutParams.MATCH_PARENT, context.dpFromDimen(R.dimen.size_1))
        // lineLP.leftMargin = context.dpFromDimen(R.dimen.spacing_16)
        // addView(line, lineLP)

        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextSetItem)
            val title = typedArray.getString(R.styleable.TextSetItem_text_title)
            val iconRes = typedArray.getResourceId(R.styleable.TextSetItem_text_icon, 0)
            val rightTextStr = typedArray.getString(R.styleable.TextSetItem_text_right_text)
            
            itemName.text = title
            rightText.text = rightTextStr
            
            if (iconRes != 0) {
                itemIcon.setImageResource(iconRes)
                itemIcon.visibility = VISIBLE
                // 设置图标颜色为白色
                itemIcon.setColorFilter(ContextCompat.getColor(context, R.color.text_primary))
            } else {
                itemIcon.visibility = GONE
            }
            
            typedArray.recycle()
        } else {
            // 默认设置
            itemIcon.visibility = GONE
        }
    }

    /**
     * 设置标题
     */
    fun setTitle(title: String) {
        itemName.text = title
    }
    
    /**
     * 设置图标
     */
    fun setIcon(iconRes: Int) {
        if (iconRes != 0) {
            itemIcon.setImageResource(iconRes)
            itemIcon.visibility = VISIBLE
        } else {
            itemIcon.visibility = GONE
        }
    }
    
    /**
     * 设置右侧文字
     */
    fun setRightText(text: String) {
        rightText.text = text
    }
}
